﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

using DrawingScreen.Messages;
using DrawingScreen.Objects;
using libCommonClass;
using libInterface;
using System.Drawing;
using System.Xml;

namespace DrawingScreen
{
    public class CDrawingScreen
    {
        private List<CDrawingObject> m_globalDrawingObjects = new List<CDrawingObject>(); 

        private Dictionary<int, CClientDrawingScreen> dictionary = new Dictionary<int, CClientDrawingScreen>();

        public void ProcessMessage(CDrawingMessage message, IClientInfoProvider provider)
        {
            int clientID;

            if (provider != null)
            {
                clientID = provider.ClientID;
            }
            else {
                clientID = 0;
            }

            bool result = dictionary.ContainsKey(clientID);

            CClientDrawingScreen clientDrawingScreen = null;

            if (result == true)
            {
                clientDrawingScreen = dictionary[clientID];
            }
            else
            {
                clientDrawingScreen = new CClientDrawingScreen(clientID, this.m_globalDrawingObjects);
                dictionary.Add(clientID, clientDrawingScreen);
            }

            if (clientDrawingScreen != null)
            {
                clientDrawingScreen.Process(message);
            }

            if (message is CDrawingMessage_Clear_Current_Slide)
            {
                this.Clear();
            }
        }

        public void Draw(Graphics g)
        {
            for (int i = 0; i < m_globalDrawingObjects.Count; i++)
            {
                if (m_globalDrawingObjects[i] != null)
                {
                    if (m_globalDrawingObjects[i].Visible == true)
                    {
                        m_globalDrawingObjects[i].Draw(g);
                    }
                }
            }

            foreach (KeyValuePair<int, CClientDrawingScreen> entry in dictionary)
            {
                entry.Value.Draw(g);
            }
        }

        public static CDrawingScreen CreateDrawingScreen(XmlElement eleDrawingScreen)
        {
            CDrawingScreen ds = new CDrawingScreen();

            ds.m_globalDrawingObjects.Clear();

            int count = eleDrawingScreen.ChildNodes.Count;

            for (int i = 0; i < count; i++)
            {
                CDrawingObject _object = null;

                XmlElement eleObject = (XmlElement)eleDrawingScreen.ChildNodes[i];

                int type = int.Parse(eleObject.Attributes["id"].Value);

                if (type == CDrawingObject.CIRCLE_ID)
                {
                    _object = CCircle.CreateDrawingObject(eleObject);
                }
                else if (type == CDrawingObject.CLIPART_ID)
                {
                    _object = CClipArt.CreateDrawingObject(eleObject);
                }
                else if (type == CDrawingObject.ELLIPSE_ID)
                {
                    _object = CEllipse.CreateDrawingObject(eleObject);
                }
                else if (type == CDrawingObject.LINE_ID)
                {
                    _object = CLine.CreateDrawingObject(eleObject);
                }
                else if (type == CDrawingObject.RECTANGLE_ID)
                {
                    _object = CRectangle.CreateDrawingObject(eleObject);
                }

                if (_object != null)
                {
                    ds.m_globalDrawingObjects.Add(_object);
                }


            }

            return ds;
        }

        public XmlNode convertToXmlNode(XmlDocument doc, int index)
        {
            try
            {
                XmlElement eleDrawingScreen = doc.CreateElement("DRAWING_SCREEN");

                XmlAttribute attrIndex = doc.CreateAttribute("index");
                attrIndex.Value = index.ToString();
                eleDrawingScreen.Attributes.Append(attrIndex);

                for (int i = 0; i < m_globalDrawingObjects.Count; i++)
                {
                    if (m_globalDrawingObjects[i] != null)
                    {
                        if (m_globalDrawingObjects[i].Visible == true)
                        {

                            try
                            {
                                XmlNode node = m_globalDrawingObjects[i].convertToXmlNode(doc);
                                eleDrawingScreen.AppendChild(node);
                            }
                            catch
                            { 
                                
                            }
                        }
                    }
                }

                return eleDrawingScreen;
            }
            catch
            {
                return null;
            }
        }

        public void Clear()
        {
            foreach (KeyValuePair<int, CClientDrawingScreen> entry in dictionary)
            {
                entry.Value.Clear();
            }
        }

        public void UpdateClientState()
        {
            foreach (KeyValuePair<int, CClientDrawingScreen> entry in dictionary)
            {
                entry.Value.UpdateClientState();
            }

            
        }
    }
}
